﻿using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Web.Mvc;
using ShopB2C.Entity;
using ShopB2C.Enums;
using ShopB2C.Helper;
using ShopB2C.ViewModels;

namespace ShopB2C.Controllers.Manager {
    /// <summary>
    /// Types
    /// </summary>
    public partial class HelpController : BaseController {

        public ActionResult TypeList([DefaultValue(1)]int number) {
            int pageCount;
            return View(new HelpTypeViewModel() {
                HelpTypeList = _help.GetHelpTypeList(this.PageSize, number, out pageCount),
                PageNumber = number,
                Pager = this.CreatePager(Url.Action("TypeList", new { number = "{0}" }), pageCount, number)
            });
        }

        public ActionResult TypeEdit([DefaultValue(1)]int number, [DefaultValue(0)]int id) {
            int pageCount;
            List<HelpTypeEntity> listHelpType = _help.GetHelpTypeList(this.PageSize, number, out pageCount);
            return View(new HelpTypeViewModel() {
                HelpType = id == 0 ? new HelpTypeEntity() {
                    Sequence = listHelpType != null ? listHelpType.Count + 1 : 1
                } : _help.GetHelpTypeById(id),
                PageNumber = number
            });
        }

        [HttpPost]
        public ActionResult TypeEdit([DefaultValue(1)]int number, HelpTypeViewModel bvm, FormCollection fc) {
            HelpTypeEntity hte = bvm.HelpType;
            // 文件处理
            var file = Request.Files["fLogo"];
            if (file.ContentLength > 0) {
                if (file.ContentLength <= 50 * 1024) {
                    string allowExt = ".gif,.bmp,.jpg,.png,";
                    string ext = Path.GetExtension(file.FileName);
                    string oldName = Path.GetFileNameWithoutExtension(file.FileName);
                    if (allowExt.Contains(ext + ",")) {
                        string fileName = string.Format("{0}{1}{2}",
                            RandomHelper.RandomString(8, RandomType.Letter),
                            oldName.Substring(0, oldName.Length >= 8 ? 8 : oldName.Length),
                            ext);
                        string big = string.Format("{0}{1}", _path, fileName);
                        string small = string.Format("{0}s_{1}", _path, fileName);
                        file.SaveAs(big);
                        MemoryStream ms = ThumbImageHelper.CuteThumbPhoto(88, 31, big, true, true);
                        using (FileStream fs = System.IO.File.Create(small)) {
                            fs.Write(ms.ToArray(), 0, (int)ms.Length);
                            fs.Flush();
                            fs.Close();
                        }
                        if (!string.IsNullOrEmpty(hte.Photo)) {
                            System.IO.File.Delete(string.Format("{0}{1}", _path, hte.Photo));
                            System.IO.File.Delete(string.Format("{0}s_{1}", _path, hte.Photo));
                        }
                        hte.Photo = fileName;
                    }
                    else {
                        Methods.Alert("图片类型只能是以 .gif,.bmp,.jpg,.png 结尾的文件!", HttpContext);
                    }
                }
                else {
                    Methods.Alert("图片大小超过 50KB!", HttpContext);
                }
            }

            if (hte.TypesId == 0) {
                _help.HelpTypeAppend(hte);
            }
            else {
                _help.HelpTypeUpdate(hte);
            }
            return this.RedirectToAction("TypeEdit", new { number = number, id = hte.TypesId });
        }

        public ActionResult TypeDelete(int id) {
            HelpTypeEntity hte = _help.GetHelpTypeById(id);
            _help.HelpTypeDelete(id);
            // 删除图片文件
            if (!string.IsNullOrEmpty(hte.Photo)) {
                System.IO.File.Delete(string.Format("{0}{1}", _path, hte.Photo));
                System.IO.File.Delete(string.Format("{0}s_{1}", _path, hte.Photo));
            }

            return RedirectToAction("TypeList");
        }

        [HttpPost]
        public ActionResult TypeEditBatchSeq([DefaultValue(1)]int number, FormCollection fc) {
            _help.HelpTypeBatchUpdateSequence(fc["cbKey"], fc["tbSequence"]);

            return this.RedirectToAction("TypeList", new { number = number });
        }

    }
}